package com.thomas.generator.component;

import com.mysql.jdbc.StringUtils;
import com.thomas.generator.util.PropertiesUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xutao
 * @Package com.thomas.generator.component
 * @Description: ${todo}
 * @date 2019/12/11 10:52
 */
public class CommonUtil {

    public static Map<String,String> typeMap = new HashMap<String, String>();
    public static Map<String,String> jdbcTypeMap = new HashMap<String, String>();
    public static String EDCODE = PropertiesUtil.get("file.encoding");
    public static String BASE_PACKAGE_PREFIX = PropertiesUtil.get("project.package.prefix");
    public static String MODULE_NAME = PropertiesUtil.get("project.moduleName");

    private static String jdbcUrl = PropertiesUtil.get("jdbc.url");
    private static String user = PropertiesUtil.get("jdbc.user");
    private static String password = PropertiesUtil.get("jdbc.password");

    private static String table_prefix = PropertiesUtil.get("generator.tableName.removed");

    //要生成代码的表名
    private static String[] generatorTableName = null;
    public static Connection conn = null;
    static {
        typeMap.put("VARCHAR", "String");
        typeMap.put("VARCHAR2", "String");
        typeMap.put("CHAR", "String");
        typeMap.put("INT", "Integer");
        typeMap.put("DATETIME", "Date");
        typeMap.put("BIGINT", "Long");
        typeMap.put("BIT", "byte[]");
        typeMap.put("BLOB", "byte[]");
        typeMap.put("DATE", "Date");
        typeMap.put("DOUBLE", "BigDecimal");
        typeMap.put("ENUM", "String");
        typeMap.put("FLOAT", "Float");
        typeMap.put("INTEGER", "Integer");
        typeMap.put("LONGBLOB", " 	byte[]");
        typeMap.put("LONGTEXT", "String");
        typeMap.put("MEDIUMINT", "Integer");
        typeMap.put("MEDIUMTEXT", "String");
        typeMap.put("TEXT", "String");
        typeMap.put("TIME", "Time");
        typeMap.put("TIMESTAMP", "Timestamp");
        typeMap.put("TINYINT", "Boolean");
        typeMap.put("TINYTEXT", "String");
        typeMap.put("YEAR", "Date");
        typeMap.put("DECIMAL", "BigDecimal");
        typeMap.put("NVARCHAR", "String");
        typeMap.put("NCHAR", "String");
        typeMap.put("REAL", "Float");
        typeMap.put("INT IDENTITY", "Integer");
        jdbcTypeMap.put("INT IDENTITY", "INTEGER");
        jdbcTypeMap.put("INT", "INTEGER");
        jdbcTypeMap.put("DATETIME", "TIMESTAMP");
        jdbcTypeMap.put("DATE", "DATE");
        jdbcTypeMap.put("NVARCHAR", "VARCHAR");
        jdbcTypeMap.put("VARCHAR", "VARCHAR");
        jdbcTypeMap.put("VARCHAR2", "VARCHAR");
    }


    public static Connection getConnection(String jdbcDriver) {
        if(conn==null){
            try {
                System.out.println(jdbcUrl);
                System.out.println(user+"   "+ password);
                Class.forName(jdbcDriver);
                conn = DriverManager.getConnection(jdbcUrl, user, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return conn;
    }

    public static void closeConnection(){
        if(null != conn){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static String getClassName(String tableName){
			/*StringBuffer buffer = new StringBuffer();
			String[] strs = tableName.toLowerCase().split("_");
			for(String s : strs){
				buffer.append(toFirstUp(s));
			}
			tableName = buffer.toString();

		return  tableName;*/
        String tableName_temp = tableName.toUpperCase().replace(table_prefix, "");
        return convertToClassName(tableName_temp);

    }

    public static String convertToClassName(String tableName) {
        //先转成小写
        tableName = tableName.toLowerCase();
        String[] words = tableName.split("_");
        String result = toUppercase4FirstLetter(words);
        //将第一个字母转成大写
        if(result.length()>0){
            String inital = result.substring(0, 1).toUpperCase();
            result = inital+result.substring(1);
        }
        System.out.println("####################### Class Name:"+result);
        return result;
    }

    private static String toUppercase4FirstLetter(String... words){
        StringBuffer buffer = new StringBuffer();
        if(words != null && words.length > 0){
            for(int i=0;i<words.length;i++){
                String word = words[i];
                String firstLetter = word.substring(0, 1);
                String others = word.substring(1);
                String upperLetter = null;
                if(i != 0){
                    upperLetter = firstLetter.toUpperCase();
                } else {
                    upperLetter = firstLetter;
                }
                buffer.append(upperLetter).append(others);
            }
            return buffer.toString();
        }
        return "";
    }

    public static void setGeneratorTableName() {
        String tableNames  = PropertiesUtil.get("generator.tableNames");
        if(!StringUtils.isNullOrEmpty(tableNames)){
            generatorTableName = tableNames.split(",");
        }
    }

    public static String[] getGeneratorTableName() {
        return generatorTableName;
    }


    public static boolean checkTableName(String tableName){
        if(null !=generatorTableName){
            for(String s : generatorTableName){
                if(s.toUpperCase().equals(tableName.toUpperCase())){
                    return true;
                }
            }
            return false;
        }else {
            return true;
        }
    }

    public static String getFileName(String columnName){
            StringBuffer buffer = new StringBuffer();
            String[] strs = columnName.toLowerCase().split("_");
            for(String s : strs){
                buffer.append(toFirstUp(s));
            }
            return buffer.toString();

    }

    public static String toFirstUp(String str){
        if(str!=null&&!"".equals(str)){
            str = str.substring(0, 1).toUpperCase()+str.substring(1);
        }

        return str;
    }

    public static String toFirstLower(String str){
        if(str!=null&&!"".equals(str)){
            str = str.substring(0, 1).toLowerCase()+str.substring(1, str.length());
        }

        return str;
    }

    public static String getJdbcType(String type){
        String jdbcType = jdbcTypeMap.get(type.toUpperCase());
        if(jdbcType==null){
            return type.toUpperCase();
        }
        return jdbcType.toUpperCase();
    }
}
